package com.seo.service.user.role.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.seo.mapper.user.UserMapper;
import com.seo.mapper.user.role.UserRoleMapper;
import com.seo.pojo.ReturnBody;
import com.seo.service.user.menu.sdk.UserMenuService;
import com.seo.service.user.role.sdk.UserRoleService;
import com.seo.utils.RightsHelper;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Service("userRoleService")
public class UserRoleServiceImpl implements UserRoleService {

	@Autowired
	private UserRoleMapper userRoleMapper;

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private UserMenuService userMenuService;

	@Override
	public List<Map<String, Object>> getAllRoleList() {

		return userRoleMapper.getAllRoleList();
	}

	@Override
	public int updateRoleRights(Map<String, Object> params) {
		// TODO Auto-generated method stub
		return userRoleMapper.updateRoleRights(params);
	}

	@Override
	public Map<String, Object> getRoleById(Integer id) {
		// TODO Auto-generated method stub
		return userRoleMapper.getRoleById(id);
	}

	@Override
	public ReturnBody getRoleMenu(Integer roleId, String type) {
		ReturnBody rb = ReturnBody.init();
		Map<String, Object> role = userRoleMapper.getRoleById(roleId);
		if (role == null) {
			log.info("角色不存在");
			return rb.error("error", "角色不存在");
		}
		if (StringUtils.isEmpty(type) || !("add".equals(type) || "del".equals(type) || "edit".equals(type)
				|| "cha".equals(type) || "rights".equals(type))) {
			log.info("参数错误");
			return rb.error("error", "参数错误");
		}
		String rights = (String) role.get(type + "Qx");

		List<Map<String, Object>> menuList = userMenuService.getAllMenuList();
		List<Map<String, Object>> nodeList = new ArrayList<>();

		for (Map<String, Object> menu : menuList) {
			Map<String, Object> node = new HashMap<>();
			boolean selected = RightsHelper.testRights(rights, (Integer) menu.get("id"));
			menu.put("selected", selected);
			node.put("id", menu.get("id"));
			Integer parentId = (Integer) menu.get("parentId");
			String parent = parentId == 0 ? "#" : String.valueOf(parentId);
			node.put("parent", parent);
			node.put("text", menu.get("name"));
			node.put("icon", menu.get("icon"));
			Map<String, Object> state = new HashMap<>();
			state.put("opened", true);
			state.put("selected", selected);
			node.put("state", state);
			nodeList.add(node);
		}
		rb.put("nodeList", nodeList);
		return rb.success();
	}

	@Override
	public ReturnBody getUserRoleMenu(Integer roleId) {
		ReturnBody rb = ReturnBody.init();
		String rights = "";

		Map<String, Object> role = userRoleMapper.getRoleById(roleId);
		if (role == null) {
			log.info("角色不存在");
			return rb.error("error", "角色不存在");
		}

		rights = (String) role.get("rightsQx");
		rb.put("role", role);

		List<Map<String, Object>> menuList = userMenuService.getAllMenuList();

		for (Map<String, Object> menu : menuList) {
			boolean selected = true;

			selected = RightsHelper.testRights(rights, (Integer) menu.get("id"));

			menu.put("selected", selected);
		}

		rb.put("menuList", menuList);
		return rb.success();
	}

	@Override
	public ReturnBody saveRole(Map<String, Object> params) {
		ReturnBody rb = ReturnBody.init();
		int num = userRoleMapper.saveRole(params);
		if (num == 0) {
			return rb.error("error", "保存失败");
		}
		return rb.success();
	}

	@Override
	public ReturnBody updateRole(Map<String, Object> params) {
		ReturnBody rb = ReturnBody.init();
		int num = userRoleMapper.updateRole(params);
		if (num == 0) {
			return rb.error("error", "保存失败");
		}
		return rb.success();
	}

	@Override
	public ReturnBody deleteRole(Integer roleId) {
		ReturnBody rb = ReturnBody.init();
		List<Map<String, Object>> userList = userMapper.getUserByRoleId(roleId);
		if (userList != null && userList.size() > 0) {
			return rb.error("该角色下已有用户");
		}

		int num = userRoleMapper.deleteRole(roleId);

		if (num == 0) {
			return rb.error("error", "删除失败");
		}

		return rb.success();
	}

}
